package com.app114.app.android.rk;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.app114.app.android.rk.adapter.CategoryAdapter;
import com.app114.app.android.rk.conf.Conf;
import com.app114.app.android.rk.utils.DataFactory;
import com.app114.app.android.rk.utils.DeviceUtil;
import com.waps.AppConnect;

public class HomeFragment extends Fragment implements OnClickListener {

	private LinearLayout mGridLinear;
	private GridView mGridView;
	private View mRoot;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mRoot = inflater.inflate(R.layout.layout_home, container, false);
		// TextView app_info = (TextView) view.findViewById(R.id.app_info);
		// app_info.setText(DataFactory.APP_INFO + "\r\n 1 ");
		// Button browser = (Button) view.findViewById(R.id.browser);

		// browser.setOnClickListener(this);
		mGridLinear = (LinearLayout) mRoot.findViewById(R.id.gridLinear);

		mGridView = (GridView) mRoot.findViewById(R.id.gridView);

		int num = DataFactory.Category.length;
		if (DeviceUtil.isLandscape(getActivity())) {
			LayoutParams lp = (LayoutParams) mGridLinear.getLayoutParams();
			lp.width = (180 + 18) * num;
			mGridLinear.setLayoutParams(lp);
			mGridView.setNumColumns(DataFactory.Category.length % 2 == 0 ? num / 2 : num / 2 + 1);
		}

		mGridView.setAdapter(new CategoryAdapter(getActivity(), DataFactory.Category, mGridView));

		DeviceUtil.animation(getActivity(), R.anim.slide_up_in, mGridView);

		Button home_btn_recommend = (Button) mRoot.findViewById(R.id.home_btn_recommend);
		// Button home_btn_account = (Button) mRoot.findViewById(R.id.home_btn_account);
		Button home_btn_game = (Button) mRoot.findViewById(R.id.home_btn_game);

		home_btn_recommend.setText(DataFactory.home_buttons[0]);
		// home_btn_account.setText(DataFactory.home_buttons[1]);
		home_btn_game.setText(DataFactory.home_buttons[2]);
		home_btn_recommend.setOnClickListener(this);
		// home_btn_account.setOnClickListener(this) ;
		home_btn_game.setOnClickListener(this);
//		DianJinPlatform.initialize(getActivity(), Conf.DIANJING_APPID, Conf.DIANJING);

		return mRoot;
	}

	@Override
	public void onClick(View v) {
		// Intent intent = new Intent(getActivity(), Browser.class);
		// intent.setAction(Intent.ACTION_VIEW);
		// intent.setData(Uri.parse("file:///android_asset/index.html"));
		// startActivity(intent);
		switch (v.getId()) {
		case R.id.home_btn_recommend:
			// DianJinPlatform.showOfferWall(getActivity(), DianJinPlatform.Oriention.SENSOR);
			AppConnect.getInstance(getActivity()).showOffers(getActivity());
			break;
		case R.id.home_btn_account:
			break;
		case R.id.home_btn_game:
			AppConnect.getInstance(getActivity()).showGameOffers(getActivity());
			break;

		default:
			break;
		}

	}

	@Override
	public void onDestroy() {
//		DianJinPlatform.destroy();
		super.onDestroy();
	}
}
